package com.oa.ylms.apps.actions.assess;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.assess.IAssessDetailInfoService;
import com.oa.ylms.apps.business.assess.IAssessInfoService;
import com.oa.ylms.apps.business.assess.IAssessItemService;
import com.oa.ylms.apps.business.assess.IAssessModelService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.assess.IAssessInfoStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.assess.AssessDetailInfoValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;
import com.oa.ylms.apps.entity.assess.AssessItemValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class AssessInfoAction extends BasicAction {

	private IAssessInfoService assessInfoService;
	private IAssessModelService assessModelService;
	private IAssessItemService assessItemService;
	private IAssessDetailInfoService assessDetailInfoService;
	private ISectionInfoService sectionInfoService;
	private Integer states;
	private String assessName;
	private String modelName;
	private String staffName;
	private Integer yearNo = IAssessInfoStates.UNDEFINED;
	private Integer monthNo = IAssessInfoStates.UNDEFINED;
	private List<AssessInfoValue> list;
	private List<AssessItemValue> assessItemList;
	private List<AssessDetailInfoValue> assessDetailInfoList;
	private Integer method;
	private Integer assessId;
	private String assessIds;
	private AssessInfoValue assessInfo;
	private AssessModelValue assessModel;
	private Map<Integer, List<String>> mapList;
	private List<SectionInfoValue> sectionList;
	private List<Integer> listYear;
	private List<Integer> listMonth;
	private List<AssessModelValue> listAssessModel;
	private int currentYear;
	private int currentMonth;
	private String result;
	private int add = 0;
	private String data;

	public String addInfo() throws Exception {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		JSONObject json = JSONObject.fromObject(result);
		assessName = json.getString("assessName");
		int modelId = json.getInt("modelId");
		yearNo = json.getInt("yearNo");
		monthNo = json.getInt("monthNo");
		String staffIds = json.getString("staffIds");
		if (assessName.length() != 0 && modelId != 0 && staffIds.length() != 0) {
			staffIds = staffIds.substring(0, staffIds.length());
			int assessId = assessInfoService.addAssessInfo(staffInfo, yearNo,
					monthNo, assessName, modelId);
			int scoresCount = assessModelService.queryAssessModelByModelId(
					modelId).getItemIds().split(",").length;
			assessDetailInfoService.addAssessDetailInfo(assessId, staffIds,
					scoresCount);
		}
		return null;
	}

	public String editDetail() throws Exception {
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		assessModel = assessModelService.queryAssessModelByModelId(assessInfo
				.getAssessModel().getModelId());
		assessItemList = assessItemService.queryAssessItemByItemIds(assessModel
				.getItemIds());
		totalpage = assessDetailInfoService.countTpages(staffName, assessId,
				rows);
		count = assessDetailInfoService.getTotalNumber();
		if (count % rows == 0 && totalpage + 1 == currpage) {
			currpage = currpage - 1;
			totalpage = currpage;
		}
		assessDetailInfoList = assessDetailInfoService
				.queryAssessDetailInfoWithPageByAssessId(staffName, assessId,
						200, currpage);
		for (int i = 0; i < assessDetailInfoList.size(); i++) {
			String[] array = assessDetailInfoList.get(i).getAssessScores()
					.split(",");
			List<String> listMap = new ArrayList<String>();
			for (String score : array) {
				listMap.add(score);
			}
			mapList.put(
					assessDetailInfoList.get(i).getStaffInfo().getStaffId(),
					listMap);
		}
		return "edit";
	}

	public String modifyInfo() throws Exception {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		JSONObject json = JSONObject.fromObject(data);
		JSONArray jsonArray = json.getJSONArray("assessInfoArray"); 
		for(int i = 0;i<jsonArray.size();i++){
			JSONObject o = jsonArray.getJSONObject(i);	
			Integer detailId = o.getInt("detailId");
			String assessScores = o.getString("assessScores");
			assessDetailInfoService.modifyAssessDetailInfo(detailId, assessScores);
		}
		if(states==1){
			assessInfoService.modifyAssessInfo(staffInfo, assessId);
		}
		return null;
	}

	public String queryDetail() throws Exception {
		assessInfo = assessInfoService.queryAssessInfoByAssessId(assessId);
		assessModel = assessModelService.queryAssessModelByModelId(assessInfo
				.getAssessModel().getModelId());
		assessItemList = assessItemService.queryAssessItemByItemIds(assessModel
				.getItemIds());
		totalpage = assessDetailInfoService.countTpages(staffName, assessId,
				rows);
		count = assessDetailInfoService.getTotalNumber();
		if (count % rows == 0 && totalpage + 1 == currpage) {
			currpage = currpage - 1;
			totalpage = currpage;
		}
		assessDetailInfoList = assessDetailInfoService
				.queryAssessDetailInfoWithPageByAssessId(staffName, assessId,
						rows, currpage);
		for (int i = 0; i < assessDetailInfoList.size(); i++) {
			String[] array = assessDetailInfoList.get(i).getAssessScores()
					.split(",");
			List<String> listMap = new ArrayList<String>();
			for (String score : array) {
				listMap.add(score);
			}
			mapList.put(
					assessDetailInfoList.get(i).getStaffInfo().getStaffId(),
					listMap);
		}
		return "detail";
	}

	public String manager() throws Exception { 
		states = IAssessInfoStates.SAVED;
		totalpage = assessInfoService.countTpages(states, yearNo, monthNo,
				modelName, assessName, rows);
		count = assessInfoService.getTotalNumber();
		if (count % rows == 0 && totalpage + 1 == currpage) {
			currpage = currpage - 1;
			totalpage = currpage;
		}
		if (add == 1) {
			currpage = totalpage;
		}
		list = assessInfoService.queryAssessInfoWithPageByStates(states,
				yearNo, monthNo, modelName, assessName, rows, currpage);
		sectionList = sectionInfoService.queryAllSectionInfo();
		listYear = assessInfoService.getListYear();
		listMonth = assessInfoService.getListMonth();
		listAssessModel = assessModelService.queryAllAssessModel();
		Calendar c = Calendar.getInstance();
		currentYear = c.get(Calendar.YEAR);
		currentMonth = c.get(Calendar.MONTH) + 1;
		return "manager";
	}

	public String queryByChecking() throws Exception {
		int states = IAssessInfoStates.CHECKING;
		totalpage = assessInfoService.countTpages(states, yearNo, monthNo,
				modelName, assessName, rows);
		count = assessInfoService.getTotalNumber();
		if (count % rows == 0 && totalpage + 1 == currpage) {
			currpage = currpage - 1;
			totalpage = currpage;
		}
		list = assessInfoService.queryAssessInfoWithPageByStates(states,
				yearNo, monthNo, modelName, assessName, rows, currpage);
		listYear = assessInfoService.getListYear();
		listMonth = assessInfoService.getListMonth();
		return "checking";
	}

	public String queryByPassed() throws Exception {
		int states = IAssessInfoStates.PASSED;
		totalpage = assessInfoService.countTpages(states, yearNo, monthNo,
				modelName, assessName, rows);
		count = assessInfoService.getTotalNumber();
		if (count % rows == 0 && totalpage + 1 == currpage) {
			currpage = currpage - 1;
			totalpage = currpage;
		}
		list = assessInfoService.queryAssessInfoWithPageByStates(states,
				yearNo, monthNo, modelName, assessName, rows, currpage);
		listYear = assessInfoService.getListYear();
		listMonth = assessInfoService.getListMonth();
		return "passed";
	}

	public String delete() {
		try {
			assessInfoService.deleteAssessInfo(assessId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String deleteMore() {
		try {
			assessInfoService.deleteAssessInfo(assessIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public IAssessInfoService getAssessInfoService() {
		return assessInfoService;
	}

	public void setAssessInfoService(IAssessInfoService assessInfoService) {
		this.assessInfoService = assessInfoService;
	}

	public Integer getStates() {
		return states;
	}

	public void setStates(Integer states) {
		this.states = states;
	}

	public String getAssessName() {
		return assessName;
	}

	public void setAssessName(String assessName) {
		this.assessName = assessName;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public Integer getYearNo() {
		return yearNo;
	}

	public void setYearNo(Integer yearNo) {
		this.yearNo = yearNo;
	}

	public Integer getMonthNo() {
		return monthNo;
	}

	public void setMonthNo(Integer monthNo) {
		this.monthNo = monthNo;
	}

	public List<AssessInfoValue> getList() {
		return list;
	}

	public void setList(List<AssessInfoValue> list) {
		this.list = list;
	}

	public Integer getMethod() {
		return method;
	}

	public void setMethod(Integer method) {
		this.method = method;
	}

	public Integer getAssessId() {
		return assessId;
	}

	public void setAssessId(Integer assessId) {
		this.assessId = assessId;
	}

	public AssessInfoValue getAssessInfo() {
		return assessInfo;
	}

	public void setAssessInfo(AssessInfoValue assessInfo) {
		this.assessInfo = assessInfo;
	}

	public IAssessModelService getAssessModelService() {
		return assessModelService;
	}

	public void setAssessModelService(IAssessModelService assessModelService) {
		this.assessModelService = assessModelService;
	}

	public AssessModelValue getAssessModel() {
		return assessModel;
	}

	public void setAssessModel(AssessModelValue assessModel) {
		this.assessModel = assessModel;
	}

	public IAssessItemService getAssessItemService() {
		return assessItemService;
	}

	public void setAssessItemService(IAssessItemService assessItemService) {
		this.assessItemService = assessItemService;
	}

	public List<AssessItemValue> getAssessItemList() {
		return assessItemList;
	}

	public void setAssessItemList(List<AssessItemValue> assessItemList) {
		this.assessItemList = assessItemList;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public IAssessDetailInfoService getAssessDetailInfoService() {
		return assessDetailInfoService;
	}

	public void setAssessDetailInfoService(
			IAssessDetailInfoService assessDetailInfoService) {
		this.assessDetailInfoService = assessDetailInfoService;
	}

	public List<AssessDetailInfoValue> getAssessDetailInfoList() {
		return assessDetailInfoList;
	}

	public void setAssessDetailInfoList(
			List<AssessDetailInfoValue> assessDetailInfoList) {
		this.assessDetailInfoList = assessDetailInfoList;
	}

	public Map<Integer, List<String>> getMapList() {
		return mapList;
	}

	public void setMapList(Map<Integer, List<String>> mapList) {
		this.mapList = mapList;
	}

	public String getAssessIds() {
		return assessIds;
	}

	public void setAssessIds(String assessIds) {
		this.assessIds = assessIds;
	}

	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}

	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public List<Integer> getListYear() {
		return listYear;
	}

	public void setListYear(List<Integer> listYear) {
		this.listYear = listYear;
	}

	public List<Integer> getListMonth() {
		return listMonth;
	}

	public void setListMonth(List<Integer> listMonth) {
		this.listMonth = listMonth;
	}

	public int getCurrentYear() {
		return currentYear;
	}

	public void setCurrentYear(int currentYear) {
		this.currentYear = currentYear;
	}

	public int getCurrentMonth() {
		return currentMonth;
	}

	public void setCurrentMonth(int currentMonth) {
		this.currentMonth = currentMonth;
	}

	public List<AssessModelValue> getListAssessModel() {
		return listAssessModel;
	}

	public void setListAssessModel(List<AssessModelValue> listAssessModel) {
		this.listAssessModel = listAssessModel;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getAdd() {
		return add;
	}

	public void setAdd(int add) {
		this.add = add;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	 

}
